package br.fatec.ads.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.fatec.ads.dao.ClienteDAO;
import br.fatec.ads.exception.ClienteException;
import br.fatec.ads.exception.DataBaseException;
import br.fatec.ads.model.Cliente;

public class ClienteExcluir extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ClienteExcluir() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			
			Cliente cliente = new Cliente();
			ClienteDAO clienteDAO = new ClienteDAO();
		
			cliente = clienteDAO.getCliente(Integer.parseInt(request.getParameter("idClienteExcluir")));
			
			 session.setAttribute("cliente", cliente);
			 
			 request.getRequestDispatcher("cadcliente_e.jsp").forward(request, response);
		} catch (ClienteException e) {
			e.printStackTrace();
		} catch (DataBaseException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			
			Cliente cliente = new Cliente();
			cliente.setIdCliente(Integer.parseInt(request.getParameter("idClienteExcluir")));
			cliente.setUsuario(session.getAttribute("usuarioDesc").toString());
			
			ClienteDAO clienteDAO = new ClienteDAO();
			
			clienteDAO.excluirCliente(cliente);
			
			request.getRequestDispatcher("ClientePesquisar?acao=SUCESSO_EXCLUIR").forward(request, response);
		} catch (ClienteException e) {
			request.getRequestDispatcher("ClientePesquisar?acao=ERRO_EXCLUIR").forward(request, response);
			e.printStackTrace();
		} catch (DataBaseException e) {
			request.getRequestDispatcher("ClientePesquisar?acao=ERRO_EXCLUIR").forward(request, response);
			e.printStackTrace();
		}
	}
}